package com.weiqiao.mall.repository.impl;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.UpdateResult;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.repository.IGiftRepository;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description:
 * @Author: 陈忠强
 * @date: 2018/6/14.
 */
@Repository("GiftRepositoryImpl")
public class GiftRepositoryImpl extends BaseMongoDaoImpl implements IGiftRepository {
    private final static String COLLECTIONAME = "gift_card";

    @Override
    public boolean saveGift(String json) {
        Document doc = Document.parse(json);
        return this.inSertDocument(COLLECTIONAME, doc);
    }

    @Override
    public int getOrderLogCount(int company_id, Bson filter) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        return (int) dbCollection.count(filter);
    }

    @Override
    public List<String> selectList(Bson filter, Bson filterSort, int skip, int limit) {
        List<String> list = this.findFilterSortSkipLimit(COLLECTIONAME, filter, filterSort, skip, limit);
        return list;
    }

    @Override
    public String findGiftCardById(Bson bson) {
        return this.findOne(COLLECTIONAME, bson);
    }

    @Override
    public boolean updateGift(Bson filter, Document doc, boolean bool) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        UpdateResult results = dbCollection.updateOne(filter, new Document("$set", doc),
                new UpdateOptions().upsert(bool).bypassDocumentValidation(true));
        if (results.getMatchedCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Bson filter, Document doc, boolean bool) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        UpdateResult results = dbCollection.updateOne(filter, new Document("$set", doc),
                new UpdateOptions().upsert(bool).bypassDocumentValidation(true));
        if (results.getMatchedCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> getGiftCardList(Bson filter) {
        return this.findFilter(COLLECTIONAME, filter);
    }
}
